<?php

namespace App\Http\Controllers\Admin\AirLine\Base;

use App\Http\Controllers\Controller;
use App\Http\Requests\airline\base\FlightRequest;
use App\Models\airline\base\FlightModel;
use Illuminate\Http\Request;

class Flight extends Controller
{
    use FlightRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $company_id = $request->input('company_id', '');
        $flight_number = $request->input('flight_number', '');
        $create_start = $request->input('create_start', '');
        $create_end = $request->input('create_end', '');
        $route_id = $request->input('route_id', '');

        $query = FlightModel::query()->with(['create_admin', 'company', 'route']);

        if($flight_number){
            $query = $query->where('flight_number', $flight_number);
        }

        if($company_id){
            $query = $query->where('company_id', $company_id);
        }

        if($route_id){
            $query = $query->where('route_id', $route_id);
        }

        if($create_start && $create_end){
            $query = $query->whereBetween('created_at', [$create_start, $create_end]);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('sort')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'company_id' => $item->company_id,
                'company_name' => $item->company ? $item->company->name : '',
                'flight_number' => $item->flight_number,
                'sort' => $item->sort,
//                'start_port_id' => $item->start_port_id,
//                'start_port_name' => $item->start_port ? $item->start_port->name : '',
//                'arrive_port_id' => $item->arrive_port_id,
//                'arrive_port_name' => $item->arrive_port ? $item->arrive_port->name : '',
                'route_id' => $item->route_id,
                'route_name' => $item->route->code ?? '',
                'take_off_time' => $item->take_off_time,
                'landing_time' => $item->landing_time,
                'classes' => $item->classes,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

//        $find = FlightModel::where('flight_number', $params['flight_number'])->first();
//        if($find){
//            return $this->error('航班号已存在');
//        }



        $model = new FlightModel();
        $model->company_id = $params['company_id'];
        $model->flight_number = $params['flight_number'];
//        $model->start_port_id = $params['start_port_id'];
//        $model->arrive_port_id = $params['arrive_port_id'];
        $model->route_id = $params['route_id'];

        if($params['take_off_time']){
            $take_off_time = strtotime($params['take_off_time']);
            $model->take_off_time = date('H:i:s', $take_off_time);
        }

        if($params['landing_time']){
            $landing_time = strtotime($params['landing_time']);
            $model->landing_time = date('H:i:s', $landing_time);
        }

//        $model->take_off_time = $params['take_off_time'];
//        $model->landing_time = $params['landing_time'];
        $model->classes = $params['classes'];
        $model->sort = $params['sort'] ?? 0;
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

    public function update(Request $request, $id){
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = FlightModel::find($id);
        if (!$model) {
            return $this->error('数据不存在');
        }

//        if($model->flight_number != $params['flight_number']){
//            $find = FlightModel::where('flight_number', $params['flight_number'])->first();
//            if($find){
//                return $this->error('航班号已存在');
//            }
//        }

        $model->company_id = $params['company_id'];
        $model->flight_number = $params['flight_number'];
//        $model->start_port_id = $params['start_port_id'];
//        $model->arrive_port_id = $params['arrive_port_id'];
        $model->route_id = $params['route_id'];
        $model->take_off_time = $params['take_off_time'];
        $model->landing_time = $params['landing_time'];
        $model->classes = $params['classes'];
        $model->sort = $params['sort'] ?? 0;
        $model->save();

        return $this->success();
    }


    public function delete(int $id){
        $model = FlightModel::find($id);
        if(!$model) {
            return $this->error('记录不存在');
        }
        $model->delete();
        return $this->success();
    }

}
